# -*- coding: utf-8 -*-
import tensorflow as tf

# ref : https://www.cnblogs.com/qggg/p/6832342.html
# doc: https://www.tensorflow.org/api_docs/python/tf/nn/conv2d

# case 2
# 指需要做卷积的输入图像，它要求是一个Tensor，具有[batch, in_height, in_width, in_channels]这样的shape，
# 具体含义是[训练时一个batch的图片数量, 图片高度, 图片宽度, 图像通道数]，
# 注意这是一个4维的Tensor，要求类型为float32和float64其中之一
input2 = tf.Variable(tf.random_normal([1, 3, 3, 5]))

# 相当于CNN中的卷积核，它要求是一个Tensor，具有[filter_height, filter_width, in_channels, out_channels]这样的shape，
# 具体含义是[卷积核的高度，卷积核的宽度，图像通道数，卷积核个数]，要求类型与参数input相同，
# 有一个地方需要注意，第三维in_channels，就是参数input的第四维
filter = tf.Variable(tf.random_normal([1, 1, 5, 1]))

# strides -> 卷积时在图像每一维的步长，这是一个一维的向量，长度4
# padding -> string类型的量，只能是"SAME","VALID"其中之一，这个值决定了不同的卷积方式
# 当其为‘SAME’时，表示卷积核可以停留在图像边缘
# feature map = src size if padding = 'SAME'
op2 = tf.nn.conv2d(input2, filter, strides=[1, 1, 1, 1], padding='VALID')

# case 3
input3 = tf.Variable(tf.random_normal([1, 3, 3, 5]))
filter = tf.Variable(tf.random_normal([3, 3, 5, 1]))

op3 = tf.nn.conv2d(input3, filter, strides=[1, 1, 1, 1], padding='VALID')
# case 4
input4 = tf.Variable(tf.random_normal([1, 5, 5, 5]))
filter = tf.Variable(tf.random_normal([3, 3, 5, 1]))

op4 = tf.nn.conv2d(input4, filter, strides=[1, 1, 1, 1], padding='VALID')
# case 5
input5 = tf.Variable(tf.random_normal([1, 5, 5, 1]))
filter = tf.Variable(tf.random_normal([3, 3, 1, 1]))

op5 = tf.nn.conv2d(input5, filter, strides=[1, 1, 1, 1], padding='SAME')
# case 6
input = tf.Variable(tf.random_normal([1, 5, 5, 5]))
filter = tf.Variable(tf.random_normal([3, 3, 5, 7]))

op6 = tf.nn.conv2d(input, filter, strides=[1, 1, 1, 1], padding='SAME')
# case 7
input = tf.Variable(tf.random_normal([1, 5, 5, 5]))
filter = tf.Variable(tf.random_normal([3, 3, 5, 7]))

op7 = tf.nn.conv2d(input, filter, strides=[1, 2, 2, 1], padding='SAME')
# case 8
input = tf.Variable(tf.random_normal([10, 5, 5, 5]))
filter = tf.Variable(tf.random_normal([3, 3, 5, 7]))

op8 = tf.nn.conv2d(input, filter, strides=[1, 2, 2, 1], padding='SAME')

init = tf.global_variables_initializer()
with tf.Session() as sess:
    sess.run(init)
    # print("case 2")
    # print(sess.run(input2))
    # print(sess.run(op2))
    # print (input2.shape,'->',op2.shape)

    # print("case 3")
    # print(sess.run(input3))
    # print(sess.run(op3))
    # print (input3.shape,'->',op3.shape)

    print("case 4")
    print(sess.run(input4))
    print(sess.run(op4))
    print (input4.shape,'->',op4.shape)

    print("case 5")
    print(sess.run(input5))
    print(sess.run(op5))
    print (input5.shape,'->',op5.shape)

    # print("case 6")
    # print(sess.run(op6))
    # print("case 7")
    # print(sess.run(op7))
    # print("case 8")
    # print(sess.run(op8))